/**
 * DateAndTime.java.
 */
package rs.edu.fit.model;

import java.util.Calendar;

public final class DateAndTime {
	/** Date; not used to get time. */
	private Calendar date;
	/** Time. */
	private String time;
	
//	private static Logger log = Logger.getLogger("rs.edu.fit.model.DateAndTime");
	
	/** Default constructor; can not be instantized. */
	@SuppressWarnings("unused")
	private DateAndTime() {
		
	}

	/**
	 * Working constructor.
	 * 
	 * @param date
	 *            Calendar instance of the date.
	 * @param time
	 *            String reprezentation of the time.
	 */
	public DateAndTime(final Calendar date, final String time) {
		super();
		this.date = date;
		this.time = time;
		
//		BasicConfigurator.configure();
	}
	
	/**
	 * Working constructor.
	 * 
	 * @param dateString
	 *            String instance of the date.
	 * @param time
	 *            String reprezentation of the time.
	 */
	public DateAndTime(final String dateString, final String time) {
		super();
		this.date = convertDateFromString(dateString);
		this.time = time;
		
//		BasicConfigurator.configure();
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		DateAndTime dat;
		if (o instanceof DateAndTime) {
			dat = (DateAndTime) o;
			return isDateCalendarTheSame(dat.getDate()) 
				&& isTimeSame(dat.getTime()); 
		}
		return false;
	}
	
	/**
	 * Is the time same.
	 * 
	 * @param dat
	 *            DateAndTime for check.
	 * @return true if it same, false otherwise.
	 */
	public final boolean isSameTime( DateAndTime dat) {
		return time.equals( dat.getTime());
	}

	/**
	 * Convert Calendar instance to the String instance of the date. String
	 * instance is like: day/month/year.
	 * 
	 * @param date
	 *            Calendar reprezentation of the date.
	 * @return String reprezentation of the date.
	 */
	public static String convertDateFromCalendar(Calendar date) {
		final int DIFF = 1;
		final int day = date.get(Calendar.DAY_OF_MONTH);
		final int month = date.get(Calendar.MONTH) + DIFF;
		final int year = date.get(Calendar.YEAR);
		
//		log.info("convertDateFromCalendar: " + day + "/" +month+ "/" + year);
		return day + "/" + month + "/" + year;
	}

	/**
	 * Convert String date instance to the Calendar instance.
	 * 
	 * @param str
	 *            String representation of the date.
	 * @return Calendar instance of the date.
	 */
	public static Calendar convertDateFromString(String str) {
		String[] dateArray = str.split("/");
		final int DIFF = 1;
		Calendar currCalendar = Calendar.getInstance();
		currCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArray[0]));
		currCalendar.set(Calendar.MONTH, Integer.parseInt(dateArray[1]) + DIFF);
		currCalendar.set(Calendar.YEAR, Integer.parseInt(dateArray[2]));
		
		return currCalendar;
	}
	
	/**
	 * Check if String representation is the same like Calendar instance of the
	 * main object. It works by converting main class object to String.
	 * 
	 * @param dateString
	 *            String that is checking.
	 * @return true if it same, false otherwise.
	 */
	public boolean isDateStringTheSame(String dateString) {
		return convertDateFromCalendar(this.date).equals(dateString);
	}
	
	/**
	 * Check if Calendar representation is the same like Calendar instance of
	 * the main object. It works by converting both to the String object.
	 * 
	 * @param calendar
	 *            Calendar that is checking.
	 * @return true if it same, false otherwise.
	 */
	public boolean isDateCalendarTheSame(Calendar calendar) {
		return convertDateFromCalendar(this.date).equals(
				convertDateFromCalendar(calendar));
	}

	/**
	 * Check if time String is same.
	 * 
	 * @param currTime
	 *            String time to check.
	 * @return true if is same, false in otherwise.
	 */
	public boolean isTimeSame(String currTime) {
		return time.equals(currTime);
	}

	/* Only getters, setters and toString from this point. */
	
	public final Calendar getDate() {
		return date;
	}

	public final void setDate(Calendar date) {
		this.date = date;
	}

	public final String getTime() {
		return time;
	}

	public final void setTime(String time) {
		this.time = time;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "DateAndTime [date=" + date + ", time=" + time + "]";
	}
	
	
	
	

}
